/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,$interval,$state,RequestService,modelService,commonConfig,FileUploader,$localStorage) {
    //获取验证码
    //getVerifyCode();
    // $scope.img = commonConfig.$webroot+commonConfig.cloudApi+'cloud/tenant/getVerifyImage?access_token='+$localStorage.get('cloud').token;
    // $scope.getVerifyCode = function(){
    //     var time = new Date().getTime();
    //     $scope.img = commonConfig.$webroot+commonConfig.userApi+'user/user/getVerifyImage?'+time+'&access_token='+$localStorage.get('cloud').token;
    // };
    //获取图片验证码
    $scope.getVerifyImage = function(){
        RequestService.Request(commonConfig.userApi+'user/verifyimage/getVerifyImage', 'GET').then(function(res){
            // console.log(res);
            $scope.imgKey = res[0];
            $scope.img = res[1];
        },function(error){
            console.log(error);
        });
    };
    $scope.getVerifyImage();
    //验证图片验证码
    $scope.ImageCodeVerifyResult = 0;
    $scope.VerifyImageCode = function(Tenant){
        var data = {
            "key": $scope.imgKey,
            "verifyCode": Tenant.verifyCode
        };
        RequestService.Request(commonConfig.userApi+'user/verifyimage/verifyCode', 'POST',data).then(function(res){
            // console.log(res);
            $scope.ImageCodeVerifyResult = res.code;
        },function(error){
            console.log(error);
        });
    };
    //获取租户列表
    RequestService.Request(commonConfig.cloudApi+'cloud/tenanttype', 'GET').then(function(response){
        console.log(response);
        $scope.TenantTypeList = response;
    },function(error){
        console.log(error);
    });
    $scope.submitTenantForm = function(Tenant){
        Tenant.imgUrl = $scope.imgUrl;
        console.log(Tenant);
        var data = {
            "id" : 0,
            "icon" : Tenant.imgUrl,
            "job" : Tenant.Job,
            "publicSchool" : Tenant.publicSchool,
            "remark" : Tenant.Remark,
            "email" : Tenant.email,
            "host" : null,
            "inputDate" : null,
            "linkman" : Tenant.ContactName,
            "identityCard":Tenant.identityCard,
            "phone" : Tenant.Telephone,
            "status" : 0,
            "tenantTypeId" : Tenant.tenantTypeId,
            "tenantName" : Tenant.tenantName
        };
        RequestService.Request(commonConfig.cloudApi+'cloud/tenant?verifyCode='+Tenant.verifyCode+'&smsVerifyCode='+Tenant.MobileMsg, 'POST',data).then(function(response){
            console.log(response);
            //验证
            if(response.code == 0){
                $state.go('CloudPlatform.TenantRegisterResult');
            }else{
                modelService.openTipsModel(response.message);
            }
        },function(error){
            console.log(error);
        })
    };
    //获取手机验证码
    $scope.SecondFlag = true;
    $scope.Second = 60;
    $scope.checkPhoneNum = function(phoneNum){
        // console.log(phoneNum);
        var pattern = /^[1-3]\d{10}$/;
        //验证手机号是否合法
        if(pattern.test(phoneNum)){
            $scope.SecondFlag = false;
        }
    };
    //发送验证码
    $scope.getMobileMsg = function(phoneNum){
        // console.log(phoneNum);
        $scope.SecondFlag = true;
        //倒计时控制
        var timer = $interval(function(){
            $scope.Second -- ;
            if($scope.Second == 0){
                $scope.SecondFlag = false;
                $interval.cancel(timer);
                $scope.Second = 60;
            }
        },1000);
        //发送手机验证码
        RequestService.Request(commonConfig.userApi+'user/verifysmsszez/sendSmsVerifyCode?mobile='+ phoneNum+'&ip='+returnCitySN["cip"],'GET').then(function(res){
            console.log(res);
            if(res.code == 0){
                modelService.openTipsModel('短信发送成功！');
            }else{
                modelService.openTipsModel(res.message);
            }
        },function(error){
            console.log(error);
        });
    };
    //图片上传设置
    var uploader = $scope.uploader = new FileUploader({
        url: commonConfig.$webroot+commonConfig.dfsApi+'file?access_token='+$localStorage.get('cloud').token,  //固定的地址
        queueLimit: 1 //文件个数限制
        //removeAfterUpload: true //上传后删除文件
    });
    // FILTERS
    uploader.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    uploader.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
        console.info('添加文件到上传队列失败后', item, filter, options);
    };
    uploader.onAfterAddingAll = function (addedFileItems) {
        $scope.uploader.uploadAll();
        console.info('添加所选的所有文件到上传队列后', addedFileItems);
    };
    uploader.onCompleteItem = function (fileItem, response, status, headers) {
        console.info('文件上传完成后', fileItem, response, status, headers);
        // console.log(response,uploader);
        $scope.imgUrl = response.uuid;
        console.info("$scope.imgUrl:", $scope.imgUrl);
    };


};